package org.dromara.tearoom.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.tearoom.domain.bo.TerRechargeRulesBo;
import org.dromara.tearoom.domain.vo.TerRechargeRulesVo;
import org.dromara.tearoom.domain.TerRechargeRules;
import org.dromara.tearoom.mapper.TerRechargeRulesMapper;
import org.dromara.tearoom.service.ITerRechargeRulesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 充值规则Service业务层处理
 *
 * @author xch
 * @date 2025-05-07
 */
@RequiredArgsConstructor
@Service
public class TerRechargeRulesServiceImpl implements ITerRechargeRulesService {

    private final TerRechargeRulesMapper baseMapper;

    /**
     * 查询充值规则
     *
     * @param id 主键
     * @return 充值规则
     */
    @Override
    public TerRechargeRulesVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询充值规则列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 充值规则分页列表
     */
    @Override
    public TableDataInfo<TerRechargeRulesVo> queryPageList(TerRechargeRulesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TerRechargeRules> lqw = buildQueryWrapper(bo);
        Page<TerRechargeRulesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的充值规则列表
     *
     * @param bo 查询条件
     * @return 充值规则列表
     */
    @Override
    public List<TerRechargeRulesVo> queryList(TerRechargeRulesBo bo) {
        LambdaQueryWrapper<TerRechargeRules> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TerRechargeRules> buildQueryWrapper(TerRechargeRulesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TerRechargeRules> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TerRechargeRules::getId);
        lqw.eq(bo.getMinAmount() != null, TerRechargeRules::getMinAmount, bo.getMinAmount());
        lqw.eq(bo.getMaxAmount() != null, TerRechargeRules::getMaxAmount, bo.getMaxAmount());
        lqw.eq(bo.getRewardType() != null, TerRechargeRules::getRewardType, bo.getRewardType());
        lqw.eq(bo.getRewardValue() != null, TerRechargeRules::getRewardValue, bo.getRewardValue());
        lqw.eq(bo.getStartTime() != null, TerRechargeRules::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, TerRechargeRules::getEndTime, bo.getEndTime());
        return lqw;
    }

    /**
     * 新增充值规则
     *
     * @param bo 充值规则
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TerRechargeRulesBo bo) {
        bo.setDelFlag("0");
        TerRechargeRules add = MapstructUtils.convert(bo, TerRechargeRules.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改充值规则
     *
     * @param bo 充值规则
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TerRechargeRulesBo bo) {
        TerRechargeRules update = MapstructUtils.convert(bo, TerRechargeRules.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TerRechargeRules entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除充值规则信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
